<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>修改部门</title>
    <link rel="icon" th:href="@{/img/icon.ico}">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;padding-right: 10px;line-height: 38px;
        }
        .tree-checked {
            background-color: #1890ff!important;
            color: #FFFFFF!important;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form" action="" lay-filter="formDemo">
            <div class="layui-form layuimini-form">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级部门</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="pid" value="0">
                        <div id="test1" style="max-height: 640px;overflow: auto"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">部门名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" lay-reqtext="请输入部门名称" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">联系人</label>
                    <div class="layui-input-block">
                        <input type="text" name="linkUser" lay-verify="required" lay-reqtext="请输入联系人" placeholder="请输入" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">联系电话</label>
                    <div class="layui-input-block">
                        <input type="text" name="linkPhone" lay-verify="required|phone" lay-reqtext="请输入联系电话" placeholder="请输入" class="layui-input" maxlength="11">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">排序</label>
                    <div class="layui-input-block">
                        <input type="number" min="0" name="sort" lay-verify="required" lay-reqtext="请输入排序" placeholder="请输入" class="layui-input" value="0">
                        <tip>值越小，越靠前</tip>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="saveBtn" shiro:hasPermission="sys:dept:info:edit"> 保存 </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript" th:src="@{/js/jquery-3.2.0.min.js}"></script>
<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/js/common-table.js}"></script>

<script type="text/javascript" th:inline="javascript">

    layui.use(['form','notice', 'tree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            notice = layui.notice;

        var ctxPath = /*[[@{/}]]*/;
        var baseUrl = ctxPath + 'sys/sys-dept-info/';

        // 初始化值
        form.val("formDemo", [[${data}]]);

        redenTree();

        var demo1;

        function redenTree(){
            demo1 = tree.render({
                elem: '#test1'
                ,id: 'test1'
                ,data: JSON.parse([[${dept}]])
                ,onlyIconControl: true
                ,click: function(obj){
                    $('#test1').find('.layui-tree-txt').removeClass('tree-checked');
                    var id = obj.data.id;
                    var pid = $('input[name="pid"]').val();
                    if (id == pid) {
                        id = 0;
                    } else {
                        $(obj.elem.children('.layui-tree-entry').get(0)).find('.layui-tree-txt').addClass('tree-checked');
                    }
                    $('input[name="pid"]').val(id);
                }
            });
            // 上级部门回显
            var pid = [[${data.pid}]];
            if(0 != pid){
                $($('#test1 div[data-id="' + pid + '"]').children('.layui-tree-entry').get(0)).find('.layui-tree-txt').addClass('tree-checked');
            }
        }

        // 监听提交
        form.on('submit(saveBtn)', function (res) {
            var data = res.field;
            Asurplus.post({
                url: baseUrl + 'update',
                data: JSON.stringify(data)
            }, function () {
                setTimeout(function () {
                    var windowIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(windowIndex);
                }, 1500)
            });
            return false;
        });
    });
</script>
</body>
</html>